screenRect=get(0,'ScreenSize');
screenpixW=screenRect(3);
screenpixH=screenRect(4);
c_x=screenpixW/2;
c_y=screenpixH/2;
distance2screen=24;% inches
screenwidth=340*0.0393700787; % inches
sheightinches=270*0.0393700787; % inches
dphs = atan2(screenwidth/2,distance2screen)*180/pi;%degrees per half screen
ppd = screenpixW/2/dphs; %pixels/degree

enableCallbacks = 'PsychEyelinkDispatchCallback';
if Eyelink('Initialize', enableCallbacks) ~= 0
    clc
    disp(' ');
    status = Eyelink('InitializeDummy');
    if Eyelink('InitializeDummy', enableCallbacks) ~=0
        % Failed as well :-(
        Screen('closeall');
        ListenChar(0);
        return;
    end
end
el = EyelinkInitDefaults();%el=elglobal; %make it local and usable
%their default is the inverse of this:
el.backgroundcolour = 0;% bcoe
el.foregroundcolour = 255;% bcoe
el.msgfontcolour    = 128;% bcoe
el.imgtitlecolour   = 128;% bcoe
el.calibrationtargetcolour=el.foregroundcolour;
el.eyeimgsize=screenpixW/20+10;
%el.msgfont='Tahoma';
el.msgfontsize=12;
%el.imgtitlefont='Tahoma';
el.imgtitlefontsize=12;
EyelinkUpdateDefaults(el); % need to update it on the host

% make sure that we get gaze data from the Eyelink
Eyelink('Command','calibration_type = HV5');
Eyelink('Command', 'link_sample_data = LEFT,RIGHT,GAZE,AREA');
%Eyelink('command', 'link_sample_data  = LEFT,RIGHT,GAZE,PUPIL,AREA,BUTTON,INPUT');
Eyelink('command', 'link_event_filter = LEFT,RIGHT,SACCADE,BLINK,MESSAGE,BUTTON');
Eyelink('command', 'file_event_filter = LEFT,RIGHT,SACCADE,MESSAGE,BUTTON,INPUT');
openfile = Eyelink('openfile',['fred','.edf']);
Eyelink('command', 'add_file_preamble_text', 'Recorded by BCOE');
Eyelink('StartRecording'); % messages must be after startrecording
Eyelink('message','RETRACE_INTERVAL: 16.667')
Eyelink('message',sprintf('disttoscreen %f sheightinches %f swidthinches %f PPD %f screenpixW %f screenpixH %f',distance2screen,sheightinches,screenwidth,ppd,screenpixW,screenpixH));
abs_start_time=getsecs;
ttext='start recording';
%DrawFormattedText(w,ttext,'center','center',c.GREEN);
%screen('flip',w);
fprintf('%s\n',ttext)
pause(1)

for tr = 1:10
    %eyelink('message','TRIAL_START'); %E.messages(starloc),diff(E.mestimes(starloc))
    Eyelink('message', 'TRIAL_START: %d',tr);
    xy=rand(1,2);
    x=round(xy(1)*screenpixW);
    y=round(xy(2)*screenpixH);
    
    eyelink('command','draw_filled_box %d %d %d %d %d',round(x-10),round(y-10),round(x+10),round(y+10),10);
    %Screen('FillOval', w ,c.RED ,[round(x-10),round(y-10),round(x+10),round(y+10)] );
    %screen('flip',w);
    eyelink('message',sprintf('FillOval (%d %d %d)',x,y,10)); %E.mestimes(end_loc)
    %eyelink('command','draw_filled_box %d %d %d %d %d',round(windowRect(RectRight)/2-50),round(windowRect(RectBottom)/2-50),(windowRect(RectRight)/2+50),round(windowRect(RectBottom)/2+50),10);
    tic
    while toc<2
        [keyIsDown,secs,keyCode] = KbCheck; % Warning: mxSetLogical is deprecated and will not be supported in the future. Please refer to the Release Notes for information on updating your code
        %warning on MATLAB:DeprecatedLogicalAPI
        if keyIsDown==1
            if lastkeyCode==0
                keyCode=max([0;find(keyCode, 1 )]);
                lastkeyCode=keyCode;
                switch keyCode
                    case 53% INT_5 on pcwin
                        Eyelink('message', sprintf('keyCode: %d: TRtrigger:  %1.02f',keyCode,(getsecs-abs_start_time)*1000));% requires SPRINTF
                        fprintf('keyCode: %d: TRtrigger:  %1.02f\n',keyCode,(getsecs-abs_start_time)*1000);
                    case 49 % keyboard 1! %blue(left) button on right box
                        Eyelink('message', sprintf('keyCode: %d: Left: %1.02f ',keyCode,(getsecs-abs_start_time)*1000));% requires SPRINTF
                        fprintf('keyCode: %d: Left:  %1.02f\n',keyCode,(getsecs-abs_start_time)*1000);
                        %case 50 % keyboard 2@
                        %case 51 %keyboard 3#
                    case 52 % keyboard 4$ %red(right) button on right box
                        Eyelink('message', sprintf('keyCode: %d: Right: %1.02f',keyCode,(getsecs-abs_start_time)*1000));% requires SPRINTF
                        fprintf('keyCode: %d: Right:  %1.02f\n',keyCode,(getsecs-abs_start_time)*1000);
                end
            end %if lastkeyCode==0
        else% if keyIsDown==1
            lastkeyCode=0;% anti hammering
        end
    end
    eyelink('message','TRIAL_END'); %E.mestimes(end_loc)
end
%screen('flip',w);
pause(1)

Eyelink('stoprecording');
ttext='stop recording';

Eyelink('ReceiveFile')